#!/usr/bin/python
# Copyright (C) 2011-2013 Zentyal S.L.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the Lesser GNU General Public License as
# published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# Lesser GNU General Public License for more details.
#
# You should have received a copy of the Lesser GNU General Public
# License along with This program; if not, write to the
#	Free Software Foundation, Inc.,
#	59 Temple Place, Suite 330,
#	Boston, MA  02111-1307
#	USA

import sys
sys.path += ['../common', '../adsync', '../export']

import gtk, tarfile, os, webbrowser
import dhcp, dns
from util import *
from zentyal_pwdsync_common import *

DOC_URL = "http://www.zentyal.org/migration-tool"

class MigrationApp(object):
    def on_adsyncEnabled_clicked(self, widget, data=None):
        set_adsync_status(self.adsyncEnabled.get_active())

    def on_mainWindow_destroy(self, widget, data=None):
        gtk.main_quit()

    def on_okButton_clicked(self, widget, data=None):
        set_passwdhk_defaults()
        set_passwdhk_confkey("host", self.host.get_text())
        set_passwdhk_confkey("port", self.port.get_text())
        set_passwdhk_confkey("secret", self.secret.get_text())
        gtk.main_quit()

    def on_exportButton_clicked(self, widget, data=None):
        # TODO: error dialog if no checkboxes are selected
        self.fileSaveDialog.set_current_name("")
        self.fileSaveDialog.run()

    def on_saveButton_clicked(self, widget, data=None):
        path = self.fileSaveDialog.get_filename()
        self.fileSaveDialog.hide()
        if not path:
            return
        tar = tarfile.open(path + ".tar.gz", "w:gz")
        if self.exportDHCP.get_active():
            dhcp.export("dhcp.yaml")
            tar.add("dhcp.yaml")
        if self.exportDNS.get_active():
            dns.export("dns.yaml")
            tar.add("dns.yaml")
        tar.close()

    def on_cancelSaveButton_clicked(self, widget, data=None):
        self.fileSaveDialog.hide()

    def on_aboutButton_clicked(self, widget, data=None):
        self.about.show()

    def on_aboutDialog_response(self, widget, data=None):
        self.about.hide()

    def on_activate_link(self, widget, data=None):
        webbrowser.open_new_tab(data)
        return True

    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file(executable_path() + "/migration.xml")
        builder.connect_signals(self)
        self.window = builder.get_object("mainWindow")
        self.about = builder.get_object("aboutDialog")
        self.fileSaveDialog = builder.get_object("fileSaveDialog")
        self.adsyncEnabled = builder.get_object("adsyncEnabled")
        self.adsyncEnabled.set_active(get_adsync_status())
        self.host = builder.get_object("hostEntry")
        self.host.set_text(get_passwdhk_confkey("host"))
        self.port = builder.get_object("portEntry")
        self.port.set_text(get_passwdhk_confkey("port"))
        self.secret = builder.get_object("secretEntry")
        self.secret.set_text(get_passwdhk_confkey("secret"))
        self.exportDHCP = builder.get_object("exportDHCP")
        self.exportDNS = builder.get_object("exportDNS")
        self.adsyncHelp2 = builder.get_object("adsyncHelpLabel2")
        self.adsyncHelp2.set_markup('For more info, please go to <a href="' + DOC_URL + '">' + DOC_URL + '</a>')

if __name__ == "__main__":
    app = MigrationApp()
    app.window.set_title('Zentyal Migration Tool')
    app.window.show()
    gtk.main()
